<?php

class Model_Orm_Movie_Tests extends \PHPUnit_Framework_TestCase
{
    public static function tearDownAfterClass()
    {
    }

    public function test_mov_1() {
        $data = array(
            'Title_Lv' => 'Sabotāža',
            'Title_En' => 'Sabotage',
            'Title_Original' => 'Sabotage',
            'Year' => 2014,
            'Duration' => 119,
            'Age_Restriction' => 16,
            'Trailer_Link' => 'FeHpSdQSH0c',
            'Description_Lv' => 'ASV narkotiku apkarošanas pārvaldes elites vienība darbojas nevainojami. Tajā dien labākie no labākajiem. Līderis Džons Vortons (A. Švarcenegers) nav guvis cieņu ar tukšu salmu kulšanu. Viņš uzticas savējiem, un viņi uzticas cits citam. Vienība savā ziņā kļuvusi par ģimeni. Tā arī turpinātos, ja kāda operācija nebūtu visu sabojājusi.',
            'Description_En' => 'Members of an elite DEA task force find themselves being taken down one by one after they rob a drug cartel safe house.',
            'Poster' => 'poster_sabotage.jpg'
        );

        $validation = Model_Orm_Movie::validate('add_mov_1');

        if($validation->run($data) == false) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        } else {
            $new_movie = new Model_Orm_Movie();
            $new_movie->title_lv = $validation->validated("Title_Lv");
            $new_movie->title_en = $validation->validated("Title_En");
            $new_movie->title_original = $validation->validated("Title_Original");
            $new_movie->description_lv = $validation->validated("Description_Lv");
            $new_movie->description_en = $validation->validated("Description_En");
            $new_movie->age_restriction = $validation->validated("Age_Restriction");
            $new_movie->year = $validation->validated("Year");
            $new_movie->duration = $validation->validated("Duration");
            $new_movie->video_id = $validation->validated("Trailer_Link");
            $new_movie->poster_large = "poster_" . $validation->validated("Poster");
            $new_movie->poster_small = "small_" . $validation->validated("Poster");
            $new_movie->save();
        }
    }
    
    public function test_mov_2() {
        $data = array(
            'Title_Lv' => 'Asa sižeta filma Sabotāža',
            'Title_En' => 'Sabotage',
            'Title_Original' => 'Sabotage',
            'Year' => 2014,
            'Duration' => 122,
            'Age_Restriction' => 16,
            'Trailer_Link' => 'FeHpSdQSH0c',
            'Description_Lv' => 'Asa sižeta filma par ASV specvienību.',
            'Description_En' => 'Members of an elite DEA task force find themselves being taken down one by one after they rob a drug cartel safe house.',
            'Poster' => 'poster_sabotage.jpg'
        );

        $validation = Model_Orm_Movie::validate('edit_mov_2_v2');

        if($validation->run($data) == false) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        } else {
            $new_movie = Model_Orm_Movie::find('last');
            $new_movie->title_lv = $validation->validated("Title_Lv");
            $new_movie->description_lv = $validation->validated("Description_Lv");
            $new_movie->duration = $validation->validated("Duration");
            $new_movie->save();
        }
    }
    
    public function test_mov_3() {
        $data = array(
            'Movie_ID' => 1,
            'Auditorium_nr' => 1,
            'Start_timestamp' => 1402153200,
            'End_timestamp' => 1402160400,
            'Price' => 5.50
        );

        $validation = Model_Orm_Show::validate('add_mov_3');

        if($validation->run($data) === false) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        } else {
            $new_show = new Model_Orm_Show();
            $new_show->movie_id = $validation->validated("Movie_ID");
            $new_show->auditorium_id = $validation->validated("Auditorium_ID");
            $new_show->start_time = $validation->validated("Start_timestamp");
            $new_show->end_time = $validation->validated("End_timestamp");
            $new_show->price = $validation->validated("Price");
            $new_show->save();   
        }
    }
    
    public function test_mov_4() {
        $data = array(
            'Movie_ID' => 1,
            'Auditorium_nr' => 1,
            'Start_timestamp' => 1402153400,
            'End_timestamp' => 1402160600,
            'Price' => 6.50
        );

        $validation = Model_Orm_Show::validate('edit_mov_4');

        if($validation->run($data) === false) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        } else {
            $show = Model_Orm_Show::find('last');
            $show->start_time = $validation->validated("Start_timestamp");
            $show->end_time = $validation->validated("End_timestamp");
            $show->price = $validation->validated("Price");
            $show->save();   
        }
    }
    
    public function test_mov_5() {
        $show = Model_Orm_Show::find('last');
        
        if(isset($show)) {
            $show->delete();
        }
    }
    
    public function test_mov_6() {
        $data = array(
            'Genre_Lv' => 'Komēdija',
            'Genre_En' => 'Comedy'
        );

        $validation = Model_Orm_Genre::validate('add_mov_6');

        if($validation->run($data) == false) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        } else {
            $new_genre = new Model_Orm_Genre();
            $new_genre->genre_lv = $validation->validated("Genre_Lv");
            $new_genre->genre_en = $validation->validated("Genre_En");

            $new_genre->save();
        }
    }
    
    public function test_mov_7() {
        $data = array(
            'Genre_Lv' => 'Piedzīvojumu',
            'Genre_En' => 'Adventure'
        );

        $validation = Model_Orm_Genre::validate('edit_mov_7');

        if($validation->run($data) == false) {
            echo implode(' * ', e((array) $validation->error()));
        } else {
            $genre = Model_Orm_Genre::find('last');
            $genre->genre_lv = $validation->validated("Genre_Lv");
            $genre->genre_en = $validation->validated("Genre_En");
            $genre->save();
        }
    }
    
    public function test_mov_8() {
        Model_Orm_Genre::find('last')->delete();
    }
    
    public function test_mov_9() {
        $data = array(
            'Movie_ID' => 1,
            'Review' => 'Iesaku noskatīties šo filmu!',
            'Rating' => 4
        );

        $validation = Model_Orm_Review::validate('add_mov_8');

        if($validation->run($data) == false) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        } else {
            $new_review = new Model_Orm_Review();
            $new_review->movie_id = $validation->validated("Movie_ID");
            $new_review->user_id = 1;
            $new_review->rating = $validation->validated("Rating");
            $new_review->review = $validation->validated("Review");
            $new_review->date = time();

            $new_review->save();
        }
    }
}